//
//  STOrderListView.m
//  store
//
//  Created by zhouzhouzhou on 2018/9/27.
//  Copyright © 2018年 zhou. All rights reserved.
//

#import "STOrderListView.h"
#import "STOrderCell.h"
#import "UIButton+ZYHblock.h"

@implementation STOrderListView


-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    tableFrame = frame;
    self.backgroundColor=[UIColor clearColor];
    return self;
}

-(void)loadOrderListView{
    if (!orderListView) {
        orderListView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        orderListView.delegate = self;
        orderListView.dataSource = self;
        orderListView.separatorStyle = UITableViewCellSelectionStyleNone;
        orderListView.backgroundColor = [UIColor clearColor];
        //    orderListView.tableFooterView = [UIView new];
        orderListView.sectionHeaderHeight = 10;
        WeakSelf
        orderListView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf updataNewOrderList];
        }];
        
        orderListView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf addMoreOrderList];
        }];
        [self addSubview:orderListView];
    }else{
        [orderListView reloadData];
    }
}

//下拉刷新
-(void)updataNewOrderList{
    [orderListView.mj_header beginRefreshing];
    if([self.orderDelegate respondsToSelector:@selector(orderListViewLoadNewData)]){
        [self.orderDelegate orderListViewLoadNewData];
    }else{
        [self endRefresh];
    }
}

////上划加载更多

-(void)addMoreOrderList{
    [orderListView.mj_footer beginRefreshing];
    if([self.orderDelegate respondsToSelector:@selector(orderListViewAddMoreData)]){
        [self.orderDelegate orderListViewAddMoreData];
    }else{
        [self endRefresh];
    }
}

-(void)endRefresh{
    [orderListView.mj_footer endRefreshing];
    [orderListView.mj_header endRefreshing];
}

#pragma mark - tableView delegate


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.orderListArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  275;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 10;
//}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    if (section%2) {
//        return 44;
//    }
//    return 0;
//}
//
//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView * headerView = [UIView new];
//    headerView.backgroundColor = [UIColor clearColor];
//    return headerView;
//}
//
//- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//
//    if (section%2) {
//        UIView * footerView = [UIView new];
//        footerView.backgroundColor = [UIColor whiteColor];
//        UIButton * detailBtn = [[UIButton alloc]init];
//        detailBtn.frame =CGRectMake(SCREEN_WIDTH-80-8, (44-30)/2, 80, 30);
//        [detailBtn setTitle:@"查看详情" forState:UIControlStateNormal];
//        [detailBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
////        [detailBtn addTarget:self action:@selector(actionTurnToOrderDetail:) forControlEvents:UIControlEventTouchUpInside];
//        detailBtn.layer.borderColor = [UIColor grayColor].CGColor;
//        detailBtn.layer.borderWidth = 1;
//        [footerView addSubview:detailBtn];
//        detailBtn.didSelectBtn = ^(UIButton * btn) {
//            [self actionTurnToOrderDetail:section];
//        };
//        return footerView;
//    }
//    return nil;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * identifier = @"orderCell";
    STOrderCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[STOrderCell alloc]init];
    }
    [cell setOrderModel:[_orderListArr objectAtIndex:indexPath.row] type:_selType];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.indexL.text = [NSString stringWithFormat:@"%zd",indexPath.row];
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.orderDelegate respondsToSelector:@selector(orderListViewSelIndexPath:)]) {
        [self.orderDelegate orderListViewSelIndexPath:indexPath];
    }
}

-(void)actionTurnToOrderDetail:(NSInteger)section{
//    UITableViewCell * cell = (UITableViewCell *)sender.superview.superview.superview;
//    NSIndexPath * indexPath = [orderListView indexPathForCell:cell];
    if ([self.orderDelegate respondsToSelector:@selector(orderListViewDetailSection:)]) {
        [self.orderDelegate orderListViewDetailSection:section];
    }
}

@end
